using UnityEngine;

public class car : MonoBehaviour
{
	public bool controlled = true;

	public bool fourWheelDrive;

	public Transform centerOfMass;

	public Transform steerWheel;

	public float steerAngle;

	public GameObject[] cams;

	public GameObject[] backLights;

	public GameObject hands;

	public Color color;

	public float Cdrag = 0.2f;

	public float Fdrag;

	public float maxTorque = 1000f;

	public float maxBrakeTorque = 1000f;

	public float MaxWheelRotateAngle = 14f;

	public float lowestSpeedAtSteer = 45f;

	public float lowSpeedSteerAngle = 32f;

	public float highSpeedSteerAngle = 1f;

	public float torque;

	public float steerWheelAngle;

	public float steerWheelRotateFactor = 1f;

	public bool braked;

	public WheelCollider WheelFrontRight;

	public WheelCollider WheelFrontLeft;

	public WheelCollider WheelRearRight;

	public WheelCollider WheelRearLeft;

	public GameObject wheelFR;

	public GameObject wheelFL;

	public GameObject wheelRR;

	public GameObject wheelRL;

	private float speed;

	private float oldAngle;

	private float newAngle;

	private float timer;

	private float angleChangeTimer;

	private float steerwheelOffset;

	private int i;

	private Quaternion rot;

	private void Start()
	{
		GetComponent<Rigidbody>().centerOfMass = centerOfMass.localPosition;
		color = backLights[0].GetComponent<Renderer>().material.GetColor("_TintColor");
		rot = steerWheel.transform.rotation;
		hands.SetActive(value: false);
		for (int i = 1; i < cams.Length; i++)
		{
			cams[i].SetActive(value: false);
		}
	}

	private void Update()
	{
		if (controlled)
		{
			CameraSwitch();
			control();
		}
	}

	private void FixedUpdate()
	{
		if (controlled)
		{
			speed = GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
			HandBrake();
		}
		Fdrag = Cdrag * Mathf.Pow(speed, 2f);
		WheelOffset(wheelFL, WheelFrontLeft);
		WheelOffset(wheelFR, WheelFrontRight);
		WheelOffset(wheelRL, WheelRearLeft);
		WheelOffset(wheelRR, WheelRearRight);
		for (int i = 0; i < backLights.Length; i++)
		{
			backLights[i].GetComponent<Renderer>().material.SetColor("_TintColor", color);
		}
	}

	private void control()
	{
		torque = maxTorque * UnityEngine.Input.GetAxis("Vertical");
		if (UnityEngine.Input.GetAxis("Vertical") < 0f && speed > 20f)
		{
			torque = 0f;
		}
		torque -= Fdrag;
		WheelRearRight.motorTorque = torque;
		WheelRearLeft.motorTorque = torque;
		if (fourWheelDrive)
		{
			WheelFrontLeft.motorTorque = torque;
			WheelFrontRight.motorTorque = torque;
		}
		SteerWheelControl();
	}

	private void SteerWheelControl()
	{
		if (angleChangeTimer < 0f)
		{
			steerwheelOffset = UnityEngine.Random.Range(-0.4f, 0.4f) * speed;
			angleChangeTimer += UnityEngine.Random.Range(0.2f, 1f);
		}
		float t = speed / 3.6f / lowestSpeedAtSteer;
		float num = Mathf.Lerp(lowSpeedSteerAngle, highSpeedSteerAngle, t);
		num *= UnityEngine.Input.GetAxis("Horizontal");
		WheelFrontRight.steerAngle = num;
		WheelFrontLeft.steerAngle = num;
		timer += Time.deltaTime * steerWheelRotateFactor;
		newAngle = Mathf.Lerp(oldAngle, num * 10f + steerwheelOffset, timer);
		steerWheelAngle = newAngle;
		steerWheel.transform.localRotation = rot * Quaternion.Euler(new Vector3(0f, steerWheelAngle, 0f));
		oldAngle = steerWheelAngle;
		if (oldAngle == newAngle)
		{
			timer = 0f;
		}
		angleChangeTimer -= Time.deltaTime;
	}

	private void WheelOffset(GameObject model, WheelCollider collider)
	{
		collider.GetWorldPose(out Vector3 pos, out Quaternion quat);
		model.transform.position = pos;
		model.transform.rotation = quat;
	}

	private void HandBrake()
	{
		if (Input.GetButton("Jump") || (UnityEngine.Input.GetAxis("Vertical") == 0f && (int)speed == 0))
		{
			braked = true;
		}
		else
		{
			braked = false;
		}
		if (braked)
		{
			WheelRearLeft.brakeTorque = maxBrakeTorque;
			WheelRearRight.brakeTorque = maxBrakeTorque;
			WheelRearRight.motorTorque = 0f;
			WheelRearLeft.motorTorque = 0f;
			if (fourWheelDrive)
			{
				WheelFrontLeft.brakeTorque = maxBrakeTorque;
				WheelFrontRight.brakeTorque = maxBrakeTorque;
				WheelFrontLeft.motorTorque = 0f;
				WheelFrontRight.motorTorque = 0f;
			}
			color.a = 1f;
		}
		else
		{
			WheelRearLeft.brakeTorque = 0f;
			WheelRearRight.brakeTorque = 0f;
			WheelFrontLeft.brakeTorque = 0f;
			WheelFrontRight.brakeTorque = 0f;
			color.a = 0.5f;
		}
	}

	private void CameraSwitch()
	{
		if (Camera.main != null)
		{
			if (UnityEngine.Input.GetKeyDown(KeyCode.C))
			{
				cams[i].SetActive(value: false);
				i++;
				if (i >= cams.Length)
				{
					i = 0;
				}
				cams[i].SetActive(value: true);
				hands.SetActive(i == 1);
			}
			if (i == 1)
			{
				Camera.main.transform.position = cams[i].transform.position;
				Camera.main.transform.rotation = cams[i].transform.rotation;
			}
		}
		else if (Camera.main == null)
		{
			UnityEngine.Debug.Log("please add a camera to the scene with tag 'MainCamera' ");
		}
	}
}
